var toolbar = [ {
	text : '新增',
	iconCls : 'icon-add',
	handler : function() {
		Handler.addData()
	}
}, '-', {
	text : '删除',
	iconCls : 'icon-cut',
	handler : function() {
		Handler.deleteData()
	}
}, '-', {
	text : '编辑',
	iconCls : 'icon-save',
	handler : function() {
		Handler.editData()
	}
}, '-', {
	text : '授权',
	iconCls : 'icon-save',
	handler : function() {
		Handler.authData()
	}
}, '-', {
	text : '查询',
	iconCls : 'icon-search',
	handler : function() {
		Handler.searchDataById()
	}
} ];
var toolbar1 = [ {
	text : '关联用户',
	iconCls : 'icon-save',
	handler : function() {
		Handler.linkData()
	}
} ];
//设置分页控件
$(document).ready(
		function() {
			var p = $('#tablelistLink').datagrid('getPager');
			p.pagination({
				pageSize : 10,// 每页显示的记录条数，默认为10
				pageList : [ 10, 20, 50 ],// 可以设置每页记录条数的列表
				beforePageText : '第',// 页数文本框前显示的汉字
				afterPageText : '页    共 {pages} 页',
				displayMsg : '当前显示 {from} - {to} 条记录   共 {total} 条记录'
			});
			$('#tablelistLink').datagrid({onLoadSuccess : function(data) {
				var rowData = data.rows;
				$.each(rowData,function(index,value) {
					var rowsChecked = $('#tablelist').treegrid('getSelected');
					var url = "/sysUserMenu/searchSelectUserByMenuIdAndUserId?menuId="
						+ rowsChecked.id
						+ "&userId="
						+ value.id;
					$.get(url,function(data) {
						if (data.status == 200) {$("#tablelistLink").datagrid("checkRow",index);
						}},"json");
					});
				}
			});
		});
var Handler = {
	deleteData : function() {
		var rowsChecked = $('#tablelist').treegrid('getSelected');
		if (rowsChecked == null || rowsChecked.id == null) {
			$.messager.alert('失败信息', "请选择数据", 'error');
		} else {
			$.messager.confirm('信息提示', '确认是否删除数据?', function(r) {
				if (r) {
					$.post("/sysMenu/removeMenu", {
						id : rowsChecked.id
					}, function(data, state) {
						if (data.status == 200) {
							$('#tablelist').treegrid('load');
						} else {
							$.messager.alert('失败信息', data.message, 'error');
						}
					}, "json");
				}
			});
		}
	},
	linkData : function() {
		var rowsChecked = $('#tablelist').treegrid('getSelected');
		var rows = $('#tablelistLink').datagrid('getChecked');
		var ids = [];
		for ( var index in rows) {
			var r = rows[index];
			ids.push(r.id);
		}
		var userIds = ids.join(',');
		$.messager.confirm('信息提示', '确认是否关联用户?', function(r) {
			if (r) {
				$.post("/sysUserMenu/link", {
					menuId : rowsChecked.id,
					userIds : userIds
				}, function(data, state) {
					if (data.status == 200) {
						$('#formwin1').window('close');
						$('#tablelist').treegrid('load');
					} else {
						$.messager.alert('失败信息', data.message, 'error');
					}
				}, "json");
			}
		});
	},
	searchDataById : function() {
		var rowsChecked = $('#tablelist').treegrid('getSelected');
		if (rowsChecked == null || rowsChecked.id == null) {
			$.messager.alert('失败信息', "请选择数据", 'error');
		} else {
			var url = "/sysMenu/" + rowsChecked.id + "/searchMenu";
			$.get(url, function(data) {
				if (data.status == 200) {
					$('#infoForm').form('load', data.data);
					console.log(data.data);
					$("#dlg-buttons2").css('display', 'none');
				}
			}, "json");
			openWindow("formwin", "查询页面", "icon-search");
		}
	},
	addData : function() {
		$('#infoForm').form('clear');
		$("#dlg-buttons2").css('display', 'block');
		openWindow("formwin", "新增页面", "icon-add");
	},
	authData : function() {
		$('#tablelistLink').datagrid({
			url : '/sysUser/searchSysUsers'
		});
		openWindow("formwin1", "授权页面", "icon-edit");
	},
	editData : function() {
		$('#infoForm').form('clear');
		var rowsChecked = $('#tablelist').treegrid('getSelected');
		var url = "/sysMenu/" + rowsChecked.id + "/searchMenu";
		$.get(url, function(data) {
			if (data.status == 200) {
				console.log(data.data);
				$('#infoForm').form('load', data.data);
				$("#dlg-buttons2").css('display', 'block');
			}
		}, "json");
		openWindow("formwin", "编辑页面", "icon-edit");
	},
	saveData : function() {
		var params = getFormData("infoForm");
		var url = "";
		if (params.id) {// 存在
			url = "/sysMenu/editMenu";
		} else {
			url = "/sysMenu/addMenu";
		}
		$.post(url, params, function(data, state) {
			if (data.status == 200) {
				$('#infoForm').form('clear');
				$('#formwin').window('close');

				$('#tablelist').treegrid('load');
			} else {
				$.messager.alert('失败信息', data.message, 'error');
				$('#infoForm').form('clear');
			}
		}, "json");
	}
};
function getFormData(id) {
	var data = $("#" + id).serializeArray();
	var item = "";
	var key = "";
	var value = "";
	var newData = {};
	data.forEach(function(value, index, array) {
		item = value;
		key = item.name;
		if (item.value) {
			value = item.value;
			newData[key] = value;
		}
	})
	return newData;
}

function openWindow(id, title, iconcls) {
	$('#' + id).window({
		width : 850,
		height : 400,
		top : ($("#body").height() - 400) * 0.5,
		left : ($("#body").width() - 800) * 0.5,
		shadow : true,
		collapsible : false,
		minimizable : false,
		maximizable : false,
		iconCls : iconcls,
		title : title,
		modal : true
	});
	$('#' + id).window('open');
}